package com.nghiatran.democonnection;

import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import com.nghiatran.networks.HttpHelper;
public class GetDataService extends Service  {
     public String ketqua=null,kq=null;
     
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	@Deprecated
	public void onStart(Intent intent, int startId) {
		Log.e(GetDataService.class.getName(), "start service");
		super.onStart(intent, startId);

		if (intent.getAction().equals("com.nghiatran.democonnection.ServiceDemo")) {
			Log.e(GetDataService.class.getName(), "Get data from http");
			new GetDataHttp().execute();
		}
	}

	public class GetDataHttp extends AsyncTask<Void, String, Void> {
		private int count = 0;
		private Handler handler;

		@Override
		protected void onPreExecute() {
			handler = new Handler();
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(Void... params) {
			//Load Data from here
	       for(int i=0;i<10;i++){		
			try {
					
				kq = HttpHelper.getData("http://onlineworks.vn/demolumi");
				Thread.sleep(10);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

				Log.i(GetDataHttp.class.getName(), "get data"+ kq + i);
	       }
			
			return null;
		}

		@Override
		protected void onProgressUpdate(String... values) {
			String ketqua = values[0];
			Intent intent = new Intent("com.nghiatran.DemoConnections");
			Bundle extras = new Bundle();
			extras.putString("ketqua", ketqua);
			intent.putExtras(extras);
			sendBroadcast(intent);
			super.onProgressUpdate(values);
		}

		@Override
		protected void onPostExecute(Void result) {
			Log.e(GetDataHttp.class.getName(), "finished download");
			super.onPostExecute(result);
		}

	}
}
